<!-- @author zhengjie -->
<template>
  <div class="icon-body">
    <el-input v-model="name" class="icon-search" clearable placeholder="请输入图标名称" @clear="filterIcons" @input="filterIcons">
      <i slot="suffix" class="el-icon-search el-input__icon" />
    </el-input>
    <div class="icon-list">
      <div class="list-container">
        <div v-for="(item, index) in filteredIconList" class="icon-item-wrapper" :key="index" @click="selectedIcon(item)">
          <div :class="['icon-item', { active: activeIcon === item }]">
            <svg-icon :icon-class="item" class-name="icon" style="height: 25px;width: 16px;"/>
            <span>{{ snuians(item) }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import icons from './requireIcons'
export default {
  name: 'IconSelect',
  props: {
    activeIcon: {
      type: String
    }
  },
  data() {
    return {
      name: '',
      iconList: icons
    }
  },
  computed: {
    filteredIconList() {
      return this.iconList.filter(item => this.isChineseEndsWith1(item));
    },
  },
  mounted() {
    console.log(this.filteredIconList)
  },
  methods: {
    isChineseEndsWith1(str) {
    // 1. 首先检查是否不是汉字（允许包含数字、字母等）
  const isNotChinese = !/^[\u4e00-\u9fa5]+$/.test(str);
  
  // 如果不是汉字，直接返回true
  if (isNotChinese) {
    return true;
  }
  
  // 2. 如果是汉字，检查最后一个字符是否是"1"
  return str.endsWith("1");
    },
    snuians(str) {
    // 1. 首先检查是否不是汉字（允许包含数字、字母等）
  const isNotChinese = !/^[\u4e00-\u9fa5]+$/.test(str);

      // 如果不是汉字，直接返回true
  if (str.endsWith("1")) {
    return str.slice(0, -1); 
  }
  if (isNotChinese) {
    return str;
  }

  // 2. 如果是汉字，检查最后一个字符是否是"1"
      // return str.endsWith("1");
},
    filterIcons() {
      this.iconList = icons

      if (this.name) {
        this.iconList = this.iconList.filter(item => item.includes(this.name))
      }
    },
    selectedIcon(name) {
      this.$emit('selected', this.snuians(name))
      document.body.click()
    },
    reset() {
      this.name = ''
      this.iconList = icons
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .icon-body {
    width: 100%;
    padding: 10px;
    .icon-search {
      position: relative;
      margin-bottom: 5px;
    }
    .icon-list {
      height: 200px;
      overflow: auto;
      .list-container {
        display: flex;
        flex-wrap: wrap;
        .icon-item-wrapper {
          width: calc(100% / 3);
          height: 25px;
          line-height: 25px;
          cursor: pointer;
          display: flex;
          .icon-item {
            display: flex;
            max-width: 100%;
            height: 100%;
            padding: 0 5px;
            &:hover {
              background: #ececec;
              border-radius: 5px;
            }
            .icon {
              // stroke: #4C6EE2;
              // stroke-width: 1px; /* 可能需要调整描边宽度 */
            }
            span {
              display: inline-block;
              vertical-align: -0.15em;
              fill: currentColor;
              padding-left: 2px;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
          }
          .icon-item.active {
            background: #ececec;
            border-radius: 5px;
          }
        }
      }
    }
  }
</style>
